package com.webserver.servlet;

import java.io.File;
import java.io.RandomAccessFile;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
/**
 * 用于处理登录业务
 * @author pc
 *
 */
public class LoginServlet extends HttpServlet{
	public void service(HttpRequest request,HttpResponse response) {
		System.out.println("LoginServlet:开始处理登录...");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		try (
			RandomAccessFile raf = new RandomAccessFile("user.dat","r");	
		){
			
			for(int i =0;i<raf.length()/100;i++) {
				raf.seek(i*100);				
				byte[] data = new byte[32];
				raf.read(data);
				String name = new String(data,"UTF-8").trim();	
				if(name.equals(username)) {
					//找到该登录用户，比对密码
					raf.read(data);
					String pwd = new String(data,"UTF-8").trim();			
					if(pwd.equals(password)) {
						//登录成功
						File file = new File("./webapps/myweb/login_success.html");
						response.setEntity(file);
						return;
					}
					break;
				}			
			}			
			//处理登录失败
			File file = new File("./webapps/myweb/login_fail.html");
			response.setEntity(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("LoginServlet:处理登录完毕!");
	}
}












